package com.comcast.core.eeg.eventpublication.service.ws;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.comcast.core.eeg.eventpublication.ejb.EventPublicationServiceLocal;
import com.comcast.core.eeg.eventpublication.types.PublicationOption;
import com.comcast.core.eeg.types.Result;
import com.comcast.core.eeg.types.TransactionOption;

public class EventPublicationServiceImpl implements  EventPublicationService {
    private static Log logger = LogFactory.getLog(EventPublicationServiceImpl.class);   
    
    private static final PublicationOption PUB_OPTION;
    static {
        PUB_OPTION = new PublicationOption();
        PUB_OPTION.setTransaction(TransactionOption.AUTO_COMMIT);
    }
    
    private EventPublicationServiceLocal serviceLocal = new EventPublicationServiceLocalImpl();
    
    private EventMapper mapper = new EventMapperImpl();
    
    public EventPublicationServiceImpl(){        
    }
    
    public void setEventMapper(EventMapper value) {
        mapper = value;
    }
    
    public void setEventPublicationServiceLocal(EventPublicationServiceLocal value) {
        serviceLocal = value;
    }

    public Result publish(String participant, XmlEvent event) {
        if (logger.isDebugEnabled()) {
            logger.debug("Participant: " + participant);
            logger.debug("XmlEvent: " + event.getEventDetails().getXml());
            logger.debug("XmlEvent: " + event.getEventName());
            EventProperty[] ps = event.getProperties();
            for (int i = 0; i < ps.length; i++) {
                logger.debug("name: " + ps[i].getName());
                logger.debug("value: " + ps[i].getValue());
            }
        }
        //map data and invoke local service
        return serviceLocal.publishEvent(mapper.createParticipant(participant), mapper.createEvent(event), PUB_OPTION);        
    }

    public Result batchPublish(String participant, XmlEvent[] events) {
        if (logger.isDebugEnabled()) {
            logger.debug("Participant: " + participant);
            for (int i = 0; i < events.length; i++) {
                logger.debug("XmlEvent: " + events[i].getEventDetails().getXml());
                logger.debug("XmlEvent: " + events[i].getEventName());
                EventProperty[] ps = events[i].getProperties();
                for (int j = 0; j < ps.length; j++) {
                    logger.debug("name: " + ps[j].getName());
                    logger.debug("value: " + ps[j].getValue());
                }            
            }
        }
        
        //map data and invoke local service
        return serviceLocal.publishEvents(mapper.createParticipant(participant), mapper.createEvents(events), PUB_OPTION);
    }
}